package com.example.demo.config;

import cn.dev33.satoken.exception.NotLoginException;
import com.example.demo.dto.ResponseDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseDTO<?> handleUnexpectedServer(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException {

        if(ex instanceof NotLoginException) {
            response.sendRedirect(request.getContextPath()+"/page/login");
        }
        return ResponseDTO.error(ex.getMessage());
    }

}
